package com.example.miiiitia.androiddemo.fragment.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.format.Time;
import android.util.AttributeSet;
import android.view.View;

import android.os.Handler;

import com.example.miiiitia.androiddemo.R;

import java.io.InputStream;
import java.util.Calendar;

/**
 * Created by miiiitia on 3/11/16.
 */
public class AnalogClock extends View {

    private Handler loopHandler = new Handler();

    private Calendar calendar = Calendar.getInstance();


    private float mSeconds;
    private float mMinutes;
    private float mHours;

    private Bitmap mDial;
    private Drawable mDialDrawable;
    private Drawable mHourHandDrawable;
    private Drawable mMinuteHandDrawable;
    private Drawable mSecondHandDrawable;

    private int mDialWidth;
    private int mDialHeight;


    private boolean startFlag = false;


    public AnalogClock(Context context) {
        this(context, null);
    }

    public AnalogClock(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AnalogClock(Context context, AttributeSet attrs,
                       int defStyle) {
        super(context, attrs, defStyle);

        Resources r = this.getContext().getResources();



        mDialDrawable = r.getDrawable(R.drawable.clock1);

        mDial = ((BitmapDrawable)mDialDrawable).getBitmap();

        mHourHandDrawable = (BitmapDrawable)r.getDrawable(R.drawable.hands);
        mMinuteHandDrawable = (BitmapDrawable)r.getDrawable(R.drawable.hands);
        mSecondHandDrawable = (BitmapDrawable)r.getDrawable(R.drawable.hands);

        mDialWidth = mDialDrawable.getIntrinsicWidth();
        mDialHeight = mDialDrawable.getIntrinsicHeight();
    }


    private void run(){

        loopHandler.post(tickRunnable);
    }

    private Runnable tickRunnable = new Runnable() {
        @Override
        public void run() {

            //postInvalidate();
            invalidate();

            loopHandler.postDelayed(this, 1000);

        }
    };




    @Override
    protected void onDraw(Canvas canvas){

        if(!startFlag){
            run();
            startFlag = true;
        }

        updateTimes();

        int availableWidth = mDial.getWidth();
        int availableHeight = mDial.getHeight();

        int x = availableWidth / 2;
        int y = availableHeight / 2;

        final Drawable dial = mDialDrawable;
        int w = dial.getIntrinsicWidth();
        int h = dial.getIntrinsicHeight();
        boolean scaled = false;

        if (availableWidth < w || availableHeight < h) {
            scaled = true;
            float scale = Math.min((float) availableWidth / (float) w,
                    (float) availableHeight / (float) h);
            canvas.save();

            canvas.scale(scale, scale, x, y);
        }

        boolean changed = true;

        if (changed) {

            dial.setBounds(x - (w / 2), y - (h / 2), x + (w / 2), y + (h / 2));
        }
        dial.draw(canvas);

        canvas.save();

        canvas.rotate(mHours / 12.0f * 360.0f, x, y);
        final Drawable hourHand = mHourHandDrawable;
        if (changed) {
            w = hourHand.getIntrinsicWidth();
            h = hourHand.getIntrinsicHeight();
            hourHand.setBounds(x - (w / 2), y - (h * 2 / 3), x + (w / 2), y + (h / 3));
        }
        hourHand.draw(canvas);

        canvas.restore();

        canvas.save();
        canvas.rotate(mMinutes / 60.0f * 360.0f, x, y);

        final Drawable minuteHand = mMinuteHandDrawable;
        if (changed) {
            w = minuteHand.getIntrinsicWidth();
            h = minuteHand.getIntrinsicHeight();
            minuteHand.setBounds(x - (w / 2), y - (h  * 4 / 5), x + (w / 2), y + (h / 5));
        }
        minuteHand.draw(canvas);
        canvas.restore();

        canvas.save();
        canvas.rotate(mSeconds / 60.0f * 360.0f, x, y);

        final Drawable scendHand = mSecondHandDrawable;
        if (changed) {
            w = scendHand.getIntrinsicWidth();
            h = scendHand.getIntrinsicHeight();
            scendHand.setBounds(x - (w / 2), y - h, x + (w / 2), y);
        }
        scendHand.draw(canvas);
        canvas.restore();

        if (scaled) {
            canvas.restore();
        }
    }

    private void updateTimes(){

        calendar.setTimeInMillis(System.currentTimeMillis());
        mSeconds = calendar.get(Calendar.SECOND);
        mMinutes = calendar.get(Calendar.MINUTE) + mSeconds/60;
        mHours = calendar.get(Calendar.HOUR) + mMinutes/60;
    }



}
